<template>
	<view>
		<view>
			<table-title content="续租办理表详情"></table-title>
			<view class="record-id">单号：{{processNo}}</view>
			<view class="in-center">
				<small-title content="基本资料"></small-title>
				<view class="state-bar">
					<view class="state-msg">{{state}}</view>
				</view>
			</view>
			<view class="item-table">
				<view-item name="楼盘" :value="userMsg.userBuilding"></view-item>
				<view-item name="楼层" :value="userMsg.userFloor" :isLast="true"></view-item>
			</view>
			
			<view v-if="status>=0">
				<small-title content="续租询问函"></small-title>
				<view class="download-text" @click="downloadFile(0)">{{fileList[0].fileName}}</view>
			</view>

			
			<view v-if="status>=5">
				<small-title content="合同续租申请表"></small-title>
				<view class="download-text" @click="downloadFile(1)">{{fileList[1].fileName}}</view>
			</view>
			
			<view v-if="status>=8">
				<small-title content="房屋场地使用协议"></small-title>
				<view class="download-text" @click="downloadFile(3)">{{fileList[3].fileName}}</view>
			</view>
			<view v-if="status>=9">
				<small-title content="家私配置清单"></small-title>
				<view class="download-text" @click="downloadFile(2)">{{fileList[2].fileName}}</view>							
			</view>

			<view v-if="status>=11">
				<small-title content="租赁合同"></small-title>
				<view v-for="(item,index) in fileList[4].fileName" class="download-text" @click="downloadFile(4,index)">{{fileList[4].fileName[index]}}</view>
			</view>
			
			<small-title v-if="status>=10" content="租赁到期日期"></small-title>
			<view v-if="status>=10" class="small-title">
				<uni-datetime-picker type="date" :value="endDate" start="" end="" @change="change" :disabled="true"></uni-datetime-picker>
			</view>
			<small-title content="处理进度"></small-title>
			<proccess-bar v-if="processFlag" :proccessList="proccessListDefult"></proccess-bar>
			<view v-if="status===0" class="mix-button">
				<view class="left-part">
					<button class="left-button" @click="qfConfirm(4)">确认续租</button>
				</view>
				<view class="right-part">
					<button class="right-button" @click="qfConfirm(3)">确认不续租</button>
				</view>
			</view>
			<view v-if="status===6" class="mix-button">
				<view class="left-part">
					<button class="left-button" @click="qfConfirm(7)">确认申请表</button>
				</view>
				<view class="right-part">
					<button class="right-button" @click="back">返回</button>
				</view>
			</view>
			<view v-if="status===9" class="mix-button">
				<view class="left-part">
					<button class="left-button" @click="qfConfirm(10)">确认协议</button>
				</view>
				<view class="right-part">
					<button class="right-button" @click="back">返回</button>
				</view>
			</view>
			<view style="height: 200rpx;"></view>
		</view>
		<uni-popup ref="popup" type="dialog">
		    <uni-popup-dialog mode="input" message="成功消息" title="请输入驳回理由" :duration="2000" :before-close="true" @close="close" @confirm="confirm"></uni-popup-dialog>
		</uni-popup>
		<l-file 
		    ref="lFile" 
			@up-success="upSuccess"
		></l-file>
	</view>
</template>		

<script>
	import {
		ONLINE_SERVER_ADDRESS_BASE,
		syncGetOrder
	} from "../../../util/network.js"
	import {
		processHistoryList
	} from "../../../util/common.js"
	export default{
		data() {
			return {
				processFlag:false,
				status:-1,
				recordId: -1,
				processNo:'待分配',
				state: '已受理',
				userMsg:{'userName':'','userPhone':'','userCompany':'','userBuilding':'',
					'userFloor':'','userRoom':''},
				goodList:[],
				fileList:[{'fileName':'续租询问函.docx', 'fileId':-1},
						  {'fileName':'合同续租申请表.docx', 'fileId':-1},
						  {'fileName':'家私配置清单.docx', 'fileId':-1},
						  {'fileName':'房屋场地使用协议.docx', 'fileId':-1},
						  {'fileName':[], 'fileId':[]},
				],
				nopassReasons:'审核通过',
				proccessListDefult:[],
				token:'',
				endDate:'',
				fileType:0, //0:续租询问函 1:合同续租申请表 2:家私配置清单 3:房屋场地使用协议 4:租赁合同
				//模拟数据列表
				datalistUser: [],
				//模拟初始数据
				tvalUser: "",
				//模拟数据列表
				datalistVerify: [],
				//模拟初始数据
				tvalVerify: "",
				rentUserId:-1,
			}
		},
		methods: {
			change(value){
				this.endDate = value + " 23:59:59"
				console.log(this.endDate)
			},
			open() {
				this.$refs.popup.open()
			},
			/**
			 * 点击取消按钮触发
			 * @param {Object} done
			 */
			close() {
				// TODO 做一些其他的事情，before-close 为true的情况下，手动执行 close 才会关闭对话框
				// ...
				this.$refs.popup.close()
			},
			/**
			 * 点击确认按钮触发
			 * @param {Object} done
			 * @param {Object} value
			 */
			confirm(value) {
				this.$refs.popup.close()
				if(value == ""){
					uni.showModal({
						content:'理由不能为空',
						showCancel:false
					});
					this.$refs.popup.close()
				}else{
					this.nopassReasons = "运营主管已驳回，理由：" + value
					this.verify(4)
					// TODO 做一些其他的事情，手动执行 close 才会关闭对话框
					// ...
					this.$refs.popup.close()
				}
			},
			
			upSuccess(e){
				console.log(e)
				if(this.fileType == 4){
					this.fileList[this.fileType].fileName.push(e.fileName)
					this.fileList[this.fileType].fileId.push(e.data)
				}else{
					this.fileList[this.fileType].fileName = e.fileName
					this.fileList[this.fileType].fileId = e.data
				}

			},
			
			uploadFile(status){
				this.fileType = status
				this.$refs.lFile.upload({
				    // #ifdef APP-PLUS
				    //（app端必传）nvue页面使用时请查阅nvue获取当前webview的api，当前示例为vue窗口
				    currentWebview: this.$mp.page.$getAppWebview(),
				    // #endif
				    //替换为你的上传接口地址
				    url: `${ONLINE_SERVER_ADDRESS_BASE}/file/upload`,
				    // 服务端接收附件的key
				    name: 'file',
				    //根据你接口需求自定义 (优先不传content-type,安卓端无法收到参数再传)
				    header: {
						'Authorization': this.token,
				        // 'Authorization': 'bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySW5mbyI6eyJ1c2VyTmFtZSI6',
				        // 'uid': '27682',
				        // 'client': 'app',
				        // 'accountid': 'DPOA9487'
				    },
				
				    // 限制选择附件的大小上限，默认10M
				    // maxSize: 20,
				
				    // 若需要在body单独添加附件名或附件大小如下方式传入组件：
				    // addName: '后端要的附件名称字段key,此处请勿写name的同值如(file)，会覆盖name',
				    // addSize: '后端要的附件大小字段key'
				
				    // body参数直接写key,value,如：
				    // date: '2020-1-1',
				    // key2: 'value2',
				});
			},
			
			downloadFile(fileType,index){
				let fileId = 0
				let errMsg = ""
				fileId = this.fileList[fileType].fileId
				switch(fileType) {
					 case 0:
						errMsg = "待合同专员上传"
						break;
					 case 1:
						errMsg = "待楼层管家上传"
						break;
					 case 2:
						errMsg = "待楼层管家上传"
						break;
					 case 3:
						errMsg = "待财务专员上传"
						break;
					 case 4:
						console.log(this.fileList[fileType])
						fileId = this.fileList[fileType].fileId[index]
						errMsg = "待楼层管家扫描上传"
						break;
				}
				if(!(parseInt(fileId) > 0)){
					uni.showModal({
						content:errMsg,
						showCancel:false
					})
					return 0
				}
				this.$refs.lFile.download({
					url:`${ONLINE_SERVER_ADDRESS_BASE}/file/download/` + fileId,
					token:this.token,
				})
				.then(path=>{
				    // 打开文件
				    this.$refs.lFile.open(path);
				});
			},
			
			qfConfirm(status){
				console.log("[info]","qf-confirm：" + this.recordId)
				let commentMsg = ''
				switch(status) {
				     case 3:
				        commentMsg = '租户已确认不续租'
				        break;
				     case 4:
				        commentMsg = '租户已确认续租'
				        break;
					 case 7:
				        commentMsg = '租户已确认续租申请表'
					    break;
					 case 10:
				        commentMsg = '租户已确认房屋场地使用协议和家私配置清单'
					    break;
				     default:
						uni.showModal({
							content:'输入错误',
							showCancel:false
						})
						return 0
				} 
				console.log("续租",status + " " + commentMsg)
				uni.request({
					url:`${ONLINE_SERVER_ADDRESS_BASE}/renew/qf-confirm/` + this.recordId,
					header: {
						'Authorization': this.token,
					},
					data: {
						status:status,
						commentMsg:commentMsg,
					},
					method:'POST',
					success: function (res) {
						console.log("qf-confirm成功",res);
						if(res.data.code == 200){
							uni.navigateBack()
						}
					},
					fail: (res) => {
						console.log("qf-confirm调用失败")
					}
				})
			},
			
			getOrder(){
				let a = syncGetOrder("renew/show/" + this.recordId)
				 a.then((res)=>{
					 console.log("异步",res)
					 if(res.data.code == 200){
						 console.log("详细",res.data.data)
						 this.status = parseInt(res.data.data.status)
						 switch(this.status) {
						 	 case 0:
						 		this.state = "待确认续租"
						 		break;
						 	 case 1:
						 		this.state = "待合同处理"
						 		break;
						 	 case 2:
						 		this.state = "待管家处理"
						 		break;
						 	 case 3:
						 		this.state = "用户不续租"
						 		break;
						 	 case 4:
						 		this.state = "待管家上传"
						 		break;
						 	 case 5:
						 		this.state = "待运营审核"
						 		break;
						 	 case 6:
						 		this.state = "待用户确认"
						 		break;
						 	 case 7:
						 		this.state = "待上传协议"
						 		break;
						 	 case 8:
						 		this.state = "待管家上传"
						 		break;
						 	 case 9:
						 		this.state = "待用户确认"
						 		break;
						 	 case 10:
						 		this.state = "已完成续租"
						 		break;
						 	 case 11:
						 		this.state = "已完成续租"
						 		break;
						      default:
						         this.state = "待申请"
						 } 
						 this.processNo = res.data.data.processNo
						 this.fileList[0].fileId = res.data.data.inquiryFileId
						 this.fileList[1].fileId = res.data.data.applyFileId
						 this.fileList[2].fileId = res.data.data.configFileId
						 this.fileList[3].fileId = res.data.data.treatyFileId
						 try{
						 	let tempArray = res.data.data.contractFileId.split(',')
						 	tempArray.forEach((item,index) => {
						 		this.fileList[4].fileId.push(item)
								let tempIndex = index + 1
								let temp ='租赁合同' + tempIndex + '.pdf'
								this.fileList[4].fileName.push(temp)
						 	})
						 }catch(e){
						 	//TODO handle the exception
						 }
						 this.proccessListDefult = processHistoryList(res.data.data.processDetail)
						 this.processFlag = true
						 if(this.status > 9){      //如果是合同专员已生成了租赁合同，则读取已写入的合同到期日期
							 this.endDate = res.data.data.dueTime.replace("T"," ")
						 }
					}
				})
			},
			
			back(){
				uni.navigateBack()
			},
		},
		onLoad(option) {
			//传入单号

			let userInfo = uni.getStorageSync('userinfo')
			if(userInfo != null){
				this.userMsg.userBuilding = userInfo.title
				this.userMsg.userFloor = userInfo.floor
				this.recordId = option.id
				this.token = uni.getStorageSync('token')
			}else{
				uni.navigateTo('../../login/login')
			}
		},
		mounted() {
			//如果是新建表单则不查询
			if(this.state != '待申请'){
				this.getOrder()
			}
		}
	}
</script>

<style>
	page{
		background-color: #F1F1F1
	}
	.record-id{
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 30rpx;
	}
	.state-bar{
		display: flex;
		align-items: center;
		color: #FFFFFF;
		background-color: #DD524D;
		width: 260rpx;
		justify-content: center;
		margin-left: auto;
		margin-right: 50rpx;
		font-size: 30rpx;
		border-radius: 50rpx;
		height: 50rpx;
	}
	.state-msg{
		display: flex;
		align-items: center;
		justify-content: center;
	}
	.item-table{
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
	.mix-button{
		display: flex;
		align-items: center;
		justify-content: center;
	}
	.left-part{
		float: right;
	}
	.right-part{
		float: left;
	}
	.left-button{
		background-color: #FF0000;
		border-radius: 50rpx 0 0 50rpx;
		color: #FFFFFF;
		width: 300rpx;
		font-size: 30rpx;
	}
	.right-button{
		background-color: #0000FF;
		border-radius: 0 50rpx 50rpx 0;
		color: #FFFFFF;
		width: 300rpx;
		font-size: 30rpx;
	}
	.in-center{
		display: flex;
		flex-direction: row;
		align-items: center;
	}
	.add-button {
		border-width: 0px;
		align-items: center;
		justify-content: center;
		margin: 30rpx 0 40rpx 250rpx;
		height: 50rpx;
		width: 250rpx;
		display: flex;
		color: #22B242;
	}
	.download-text {
	  -webkit-box-sizing: content-box;
	  -moz-box-sizing: content-box;
	  box-sizing: content-box;
	  border: none;
	  font: normal 16px/1 "Times New Roman", Times, serif;
	  color: rgba(18,25,232,1);
	  text-decoration: underline;
	  -o-text-overflow: ellipsis;
	  text-overflow: ellipsis;
	  margin-left: 60rpx;
	  margin-bottom: 10rpx;
	}
	.small-title{
		margin: 20rpx 0 20rpx 50rpx;
	}
</style>
